1147A - Hide and Seek - CodeForces Solution


graphs *1500

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long

signed main()
{
    ios::sync_with_stdio(false) ;
    cin.tie(0) ;
    cout.tie(0) ;
 
//    int t;
//    cin>>t;
//    while(t--)
    {
    	int i,n,k;
    	cin>>n>>k;
    	vector<int> fa(100006,1e9),la(100006,0);
    	for(i=1;i<=k;i++)
    	{
    		int x;
    		cin>>x;
    		fa[x]=min(fa[x],i);
    		la[x]=max(la[x],i);
		}
		int na=0;
		for(i=1;i<=n;i++)
		{
			if(fa[i]!=0&&la[i]!=0)
			{
				na++;
			}
			else
			{
				continue;
			}
			if(i==1)
			{
				if(la[2]>fa[1])
				{
					na++;
				}
				continue;
			}
			if(i==n)
			{
				if(la[n-1]>fa[n])
				{
					na++;
				}
				continue;
			}
			if(la[i-1]>fa[i])
			{
				na++;
			}
			if(la[i+1]>fa[i])
			{
				na++;
			}
		}
		cout<<3*n-2-na;
	}
    return 0;
}


Comments

Submit
0 Comments
More Questions

1469B - Red and Blue
1257B - Magic Stick
18C - Stripe
1203B - Equal Rectangles
1536A - Omkar and Bad Story
1509A - Average Height
1506C - Double-ended Strings
340A - The Wall
377A - Maze
500A - New Year Transportation
908D - New Year and Arbitrary Arrangement
199A - Hexadecimal's theorem
519C - A and B and Team Training
631A - Interview
961B - Lecture Sleep
522A - Reposts
1166D - Cute Sequences
1176A - Divide it
1527A - And Then There Were K
1618E - Singers' Tour
1560B - Who's Opposite
182B - Vasya's Calendar
934A - A Compatible Pair
1618F - Reverse
1684C - Column Swapping
57C - Array
1713D - Tournament Countdown
33A - What is for dinner
810A - Straight A
1433C - Dominant Piranha